package com.itcast.adapter;

import java.util.ArrayList;

import com.itcast.R;
import com.itcast.activity.BaseActivity.ImageCallback;
import com.itcast.util.ImageUtil;
import com.itcast.vo.HomeGallery;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class HomeGalleryAdapter extends BaseAdapter {
// = =
	private Context context;
	private ArrayList<HomeGallery> galleryList;
	private Gallery gallery;
	
	public HomeGalleryAdapter(Context context,ArrayList<HomeGallery> arrayList,Gallery gallery){
		this.context = context;
		this.galleryList = arrayList;
		this.gallery = gallery;
	}
	
	@Override
	public int getCount() {
		return Integer.MAX_VALUE;
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		ImageView imageView = new ImageView(context);
		String imageUrl = "/images/image1.png";//galleryList.get(arg0%galleryList.size()).pic;
		
		String imagePath = ImageUtil.getCacheImgPath().concat(ImageUtil.md5(imageUrl));
		imageView.setTag(imagePath);
		Bitmap bitmap = ImageUtil.loadImage(imagePath, imageUrl, new ImageCallback() {
			@Override
			public void loadImage(Bitmap bitmap, String imagePath) {
				ImageView imageView = (ImageView) gallery.findViewWithTag(imagePath);
				if(imageView!=null){
					imageView.setImageBitmap(bitmap);
				}
			}
		});
		
		if(bitmap==null){
			imageView.setImageResource(R.drawable.product_loading);
		}else{
			imageView.setImageBitmap(bitmap);
		}
		imageView.setScaleType(ImageView.ScaleType.FIT_XY);
		imageView.setBackgroundResource(R.drawable.home_backgroud);
		return imageView;
	}

}
